﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Examination.Utils
{
    public interface ICaptcha
    {
        /// <summary>
        /// 生成随机验证码
        /// </summary>
        /// <param name="codeLength"></param>
        /// <returns></returns>
        Task<string> GenerateRandomCaptchaAsync(int codeLength = 4);

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="captchaCode">验证码</param>
        /// <param name="width">宽为0将根据验证码长度自动匹配合适宽度</param>
        /// <param name="height">高</param>
        /// <returns></returns>
        Task<CaptchaResult> GenerateCaptchaImageAsync(string captchaCode, int width = 0, int height = 30);
    }

    public class Captcha: ICaptcha
    {
        private const string Letters = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";

        public Task<CaptchaResult> GenerateCaptchaImageAsync(string captchaCode, int width = 0, int height = 30)
        {
            Color[] c = { Color.FromArgb(0x66, 0xcc, 0xff), Color.FromArgb(0x66, 0xff, 0xcc), Color.FromArgb(0xcc, 0xff, 0x66), Color.FromArgb(0xcc, 0x66, 0xff), Color.FromArgb(0xff, 0xcc, 0x66), Color.FromArgb(0xff, 0x66, 0xcc) };
            string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial" };
            Bitmap image = new Bitmap(width == 0 ? captchaCode.Length * 25 : width, height);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            Random random = new Random();
            for (var i = 0; i < 100; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }
            for (int i = 0; i < captchaCode.Length; i++)
            {
                int cindex = random.Next(c.Length);
                int findex = random.Next(fonts.Length);
                Font f = new Font(fonts[findex], 15, FontStyle.Bold);
                Brush b = new SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(captchaCode.Substring(i, 1), f, b, 17 + (i * 17), ii);
            }
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Png);
            g.Dispose();
            image.Dispose();
            return Task.FromResult(new CaptchaResult
            {
                CaptchaCode = captchaCode,
                CaptchaMemoryStream = ms
            });
        }

        public Task<string> GenerateRandomCaptchaAsync(int codeLength = 4)
        {
            string[] array = Letters.Split(new[] { ',' });
            Random random = new Random();
            int temp = -1;
            string captcheCode = string.Empty;
            for (int i = 0; i < codeLength; i++)
            {
                if (temp != -1)
                {
                    random = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                int index = random.Next(array.Length);
                if (temp != -1 && temp == index)
                {
                    return GenerateRandomCaptchaAsync(codeLength);
                }
                temp = index;
                captcheCode += array[index];
            }
            return Task.FromResult(captcheCode);
        }
    }

    public class CaptchaResult
    {
        /// <summary>
        /// 验证码
        /// </summary>
        public string CaptchaCode { get; set; }

        /// <summary>
        /// 验证码内存流
        /// </summary>
        public MemoryStream CaptchaMemoryStream { get; set; }
    }
}
